home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / text / misc / how2code.txt.pp / how2code.txt
Text File  |  1994-11-17  |  12KB  |  418 lines

  1.                        How to write demos that work
  2.                        ============================
  3.  
  4.              (or How to bash the metal and get away with it)
  5.  
  6.                  by Comrade J, Share and Enjoy (retired)
  7.  
  8.  
  9. Are you fed up of downloading a new demo, running it and finding
  10. your Amiga power light sitting there flashing at you? Are you
  11. tired of the contents of your chipram flashing through the bitplane
  12. registers in a rather random fashion when you run 'Duffex's
  13. Megademo'?
  14.  
  15. Do you want demos that actually run? Because I do. I'm fed up
  16. completely with all these rubbish coders who can't do their job
  17. properly...
  18.  
  19. Now, since those long days and nights bashing away at Share and Enjoy
  20. demos I've been involved in all sorts of Amiga programming, involving
  21. new AGA machines, CD, 16-bit audio and all sorts of other
  22. wonderful things that I can't talk about, both via the OS and at a
  23. hardware level.
  24.  
  25. Most demos I've seen use similar startup code to that I was using back
  26. in 1988. Hey guys, wake up! The Amiga has changed quite a bit since
  27. then.
  28.  
  29. So. Here are some tips on what to do and what not to do:
  30.  
  31.  
  32.  
  33.  
  34.  
  35. 1. RTFM.
  36. ========
  37.  
  38. Read the F**king manuals. All of them. Borrow them off friends or from
  39. your local public library if you have to. Make sure you follow the
  40. hardware rules to the letter. If it says "Leave this bit cleared" then
  41. don't set it!
  42.  
  43. There is a lot of useful informatiton in the 'boring' OS Rom Kernal
  44. Manuals that you may be suprised to know.
  45.  
  46. Read the "General Amiga Development Guidelines" in the new (grey)
  47. Hardware Reference Manual and follow them TO THE LETTER.
  48.  
  49. Don't use self-modifying code. A common bit of code I see is:
  50.  
  51. ... in the setup code
  52.  
  53.     move.l  $6c.w,old
  54.  
  55. ... at the end of the interrupt
  56.  
  57.         movem.l    (sp)+,a0-a6/d0-d7
  58.     dc.w    $4ef9               ; jmp instruction
  59. old    dc.l    0                   ; self modifying!!!!
  60.  
  61. DONT DO THIS!
  62.  
  63. This is much better (although it still hits $6c which is not a
  64. good thing - It's much better to use AddIntServer() to call your
  65. interrupts, but more on that next time....)
  66.  
  67. ... in the setup code
  68.  
  69.     move.l  $6c.w,old
  70.  
  71. ... at the end of the interrupt
  72.  
  73.         movem.l    (sp)+,a0-a6/d0-d7
  74.     move.l    old,-(sp)        ; push old address to stack
  75.         rts
  76.  
  77. ... in your data section
  78.  
  79. old    dc.l    0
  80.  
  81.  
  82. 68020 and above processors with cache enabled often barf at the first
  83. piece of code (the cache still contains the JMP 0 instruction
  84. which isn't then altered), but the second piece works fine on the '040.
  85.  
  86.  
  87.  
  88.  
  89.  
  90. 2. Proper Copper startup.
  91. =========================
  92.  
  93. IF you are going to use the copper then this is how you should set it
  94. up. The current workbench view and copper address are stored, the
  95. system frozen, and then the copper enabled. On exit the workbench
  96. view is restored.
  97.  
  98. This guarantees your demo will run on an AGA (Amiga 1200) machine,
  99. even if set into some weird screenmode before running your code.
  100.  
  101. Otherwise under AGA, the hardware registers can be in some strange states
  102. before your code runs, beware!
  103.  
  104. The LoadView(NULL) forces the display to a standard, empty position,
  105. flushing the crap out of the hardware registers: Note. There is
  106. a bug in the V39 OS on Amiga 1200/4000 and the sprite resolution is
  107. *not* reset, you will have to do this manually if you use sprites...
  108.  
  109. Two WaitTOF() calls are needed after the LoadView to wait for both the
  110. long and short frame copperlists of interlaced displays to finish.
  111.  
  112.  
  113. I'm writing this code off the top of my head, if you spot any obvious bugs
  114. email me at the address below, or leave mail to me on Bad Dreams BBS.
  115.  
  116.                 include "exec/macros.i"
  117.                 include "exec/funcdef.i"       ; keep code simple and
  118.                 include "exec/exec_lib.i"      ; easy to read - use
  119.                 include "graphics/gfxbase.i"   ; the includes!
  120.                 include "graphics/gfx_lib.i"
  121.                 include "misc/easystart.i"     ; Allows startup from
  122.                                                ; icon
  123.  
  124.                 section mycode,code            ; need not be in chipram
  125.  
  126. StartCopper:
  127.                 move.l  4.w,a6          ; get ExecBase
  128.         lea    gfxname,a1    ; graphics name
  129.                 moveq    #0,d0        ; any version
  130.         jsr    _LVOOpenLibrary(a6)
  131.         tst.l    d0
  132.         beq     End         ; failed to open? Then quit
  133.         move.l  d0,gfxbase
  134.         move.l    d0,a6
  135.         move.l  gb_ActiView(a6),wbview
  136.                     ; store current view address
  137.                                         ; gb_ActiView = 32
  138.         move.l    gb_LOFlist(a6),wbcop
  139.                     ; store current wb copper
  140.                     ; gb_LOFlist = 48
  141.  
  142.                 move.w    #0,a1        ; clears full long-word
  143.         jsr     _LVOLoadView(a6) ; Flush View to nothing
  144.  
  145.                 jsr    _LVOWaitTOF(a6) ; Wait once
  146.         jsr    _LVOWaitTOF(a6) ; Wait again.
  147.  
  148.                             ; Now you can hit the copper!
  149.  
  150.         move.l    4.w,a6
  151.                 jsr    _LVOForbid(a6)  ; Suspend multitasking!
  152.  
  153.         move.l    mycopper,$dff080 ; bang it straight in.
  154.                                          ; (DO NOT F**K WITH GFXBASE!)
  155.  
  156.                 *** DO YOUR FUNKY STUFF HERE ***
  157.  
  158.  
  159. CloseDown:    move.l    4.w,a6
  160.             jsr    _LVOPermit(a6)     ; Enable multitasking
  161.  
  162.         move.l    wbview,a1
  163.         move.l    gfxbase,a6
  164.         jsr    _LVOLoadView(a6) ; Fix view
  165.  
  166.         move.l    wbcop,$dff080     ; Kick it into life
  167.  
  168.         move.l    a6,a1
  169.             move.l    4.w,a6
  170.         jsr    _LVOCloseLibrary(a6) ; EVERYONE FORGETS THIS!!!!
  171.  
  172. End:        rts
  173.  
  174.         section mydata,data_C    ; keep data & code seperate!
  175.  
  176. copperlist      dc.w    $180,0          ; background black
  177.                 dc.w    $5c07,$fffe     ; wait for $5c07,$fffe
  178.                 dc.w    $180,$ff0       ; background yellow
  179.                 dc.w    $ffff,$fffe
  180.                 dc.w    $ffff,$fffe     ; Copper end (always do two
  181.                                         ; copper end cmds!)
  182.  
  183. wbview      dc.l    0
  184. wbcop       dc.l    0
  185. gfxbase     dc.l    0
  186. gfxname     dc.b    "graphics.library",0
  187.  
  188.  
  189. 3. Your code won't run from an icon.
  190. ====================================
  191.  
  192. You stick an icon for your new demo (not everyone uses the CLI!) and
  193. it either crashes or doesn't give back all the RAM it uses. Why?
  194.  
  195. Icon startup needs specific code to reply to the workbench message.
  196. With the excellent Hisoft Devpac assember, all you need to do is add
  197. the line
  198.  
  199.     include "misc/easystart.i"
  200.  
  201. and it magically works!
  202.  
  203. For those without Devpac, here is the relevent code:
  204.  
  205. ---------------------------------------------------------
  206.  
  207. * some startup code to make a Workbench execute look like the CLI
  208. * based loosely on RKM Vol 1 page 4-36
  209.  
  210. * Include this at the front of your program
  211. * after any other includes
  212. * note that this needs exec/exec_lib.i
  213.  
  214.     IFND    EXEC_EXEC_I
  215.     include    "exec/exec.i"
  216.     ENDC
  217.     IFND    LIBRARIES_DOSEXTENS_I
  218.     include    "libraries/dosextens.i
  219.     ENDC
  220.  
  221.  
  222.     movem.l    d0/a0,-(sp)        save initial values
  223.     clr.l    returnMsg
  224.  
  225.     sub.l    a1,a1
  226.     move.l    4.w,a6
  227.     jsr    _LVOFindTask(a6)        find us
  228.     move.l    d0,a4
  229.  
  230.     tst.l    pr_CLI(a4)
  231.     beq.s    fromWorkbench
  232.  
  233. * we were called from the CLI
  234.     movem.l    (sp)+,d0/a0        restore regs
  235.     bra    end_startup        and run the user prog
  236.  
  237. * we were called from the Workbench
  238. fromWorkbench
  239.     lea    pr_MsgPort(a4),a0
  240.     move.l    4.w,a6
  241.     jsr    _LVOWaitPort(A6)        wait for a message
  242.     lea    pr_MsgPort(a4),a0
  243.     jsr    _LVOGetMsg(A6)            then get it
  244.     move.l    d0,returnMsg        save it for later reply
  245.  
  246. * do some other stuff here RSN like the command line etc
  247.     nop
  248.  
  249.     movem.l    (sp)+,d0/a0        restore
  250. end_startup
  251.     bsr.s    _main            call our program
  252.  
  253. * returns to here with exit code in d0
  254.     move.l    d0,-(sp)        save it
  255.  
  256.     tst.l    returnMsg
  257.     beq.s    exitToDOS        if I was a CLI
  258.  
  259.     move.l    4.w,a6
  260.         jsr    _LVOForbid(a6)
  261.     move.l    returnMsg(pc),a1
  262.     jsr    _LVOPermit(a6)
  263.  
  264. exitToDOS
  265.     move.l    (sp)+,d0        exit code
  266.     rts
  267.  
  268. * startup code variable
  269. returnMsg    dc.l    0
  270.  
  271. * the program starts here
  272.     even
  273. _main
  274.  
  275. ---------------------------------------------------------
  276.  
  277.  
  278.  
  279.  
  280. 4. How do I tell if I'm running on an Amiga 1200/4000?
  281. ======================================================
  282.  
  283. Do *NOT* check library revision numbers, V39 OS can and does
  284. run on standard & ECS chipset machines (This Amiga 3000
  285. is currently running V39).
  286.  
  287. This code will check for AGA
  288.  
  289.         move.w    $dff07c,d0
  290.     and.w    #$ff,d0
  291.     cmp.w    #$f8,d0
  292.     bne.s    .notaga
  293.  
  294.     ; Do your funky AGA Stuff in here!
  295.  
  296. .notaga
  297.  
  298.  
  299.  
  300.  
  301.  
  302. 5. Use Relocatable Code
  303. =======================
  304.  
  305. If you write demos that run from a fixed